package qingke.app;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import qingke.pojo.Account;
import qingke.pojo.Catalog;
import qingke.pojo.Client;
import qingke.pojo.ClientStatus;
import qingke.pojo.ClientType;
import qingke.pojo.Order;
import qingke.pojo.OrderItem;
import qingke.pojo.Payment;
import qingke.pojo.PaymentMethod;
import qingke.pojo.PaymentStatus;
import qingke.pojo.Product;
import qingke.pojo.User;

public class OrderDao {


	public boolean isUsernameExists(String username){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		
//		User user = new User();
		Query<User> query = session.createQuery("from User where username ='" +username+"'");
		if(query.getResultList().size()==0){
			return false;
			}
		return true;	
	}
	
	public User signup(String username,String password,String email,String phone){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();

		User user = new User(username,password);
		user.setEmail(email);
		user.setPhone(phone);
		Transaction tx = session.beginTransaction();
		try {
			session.save(user);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		return login(username,password);
	}
	
	public User login(String username,String password){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		try{
			Query<User> query = session.createQuery("from User where username ='"+username+"'and password ='"+password+"'");
			List<User> users = query.getResultList();
			
			return users.get(0);
			
		}catch(Exception e){
			
		}
		return null;
		
	}
	public List<ClientType> getClientType(){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		ClientType ct = session.get(ClientType.class, 1);
		ClientType ct1 = session.get(ClientType.class, 2);
		List<ClientType> list = new ArrayList<>();
		list.add(ct);
		list.add(ct1);
		return list;
	}
	
	
	public Client setClient(User user,String name,String phone,int clientTypeId){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		
		Client client = new Client();
		client.setName(name);
		client.setPhone(phone);
		client.setClientType(getClientType().get(clientTypeId-1));
		client.setUser(user);
		Transaction tx = session.beginTransaction();
		try {
			session.save(client);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		return client;
	}
	
	public List<Product> getProduct(){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Query<Product> query = session.createQuery("from Product", Product.class);
		List<Product> products = query.getResultList();
		if(products!=null){
			return products;
		}
		return null;
	}
	
	public Order createOrder(User user ,String time){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Set<Client> clients = user.getClient();
		Iterator<Client> it = clients.iterator();
		Client client = new Client();
		if(it.hasNext())
		{
			client = it.next();
		}
		
		Order order = new Order();
		order.setClient(client);
		order.setCreateDatetime(time);
		Transaction tx = session.beginTransaction();
		try {
			session.save(order);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
//		System.out.println(order.toString1());
		return order;
	}
	
	public int getOrderId(User user ,String time){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Set<Client> clients = user.getClient();
		Iterator<Client> it = clients.iterator();
		Client client = new Client();
		if(it.hasNext())
		{
			client = it.next();
		}
		Query<Order> query = session.createQuery("from Order where createDatetime ='"+time+"' and client_id = '"+client.getId()+"'");
		List<Order> orders = query.getResultList();
		int a = 0;
		if(orders.size()!=0){
			a = orders.get(0).getId();
		}
		return a ;
	}
	
	public Product getProductFromId(int productId){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Product product = session.get(Product.class, productId);
		return product;
	}
	
	public void setOrderItem(List<OrderItem> orderItems){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		for (OrderItem orderItem : orderItems) {
			Transaction tx = session.beginTransaction();
			try {
				session.save(orderItem);
				tx.commit();
				session.clear();
			} catch (HibernateException e) {
				e.printStackTrace();
				tx.rollback();
			}
		}
	}

	public void setAccount(User user){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Set<Client> clients = user.getClient();
		Iterator<Client> it = clients.iterator();
		Client client = new Client();
		if(it.hasNext())
		{
			client = it.next();
		}
		Account account = new Account();
		account.setBalance(0);
		account.setClient(client);
		Transaction tx = session.beginTransaction();
		try {
			session.save(account);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	
	public void changeAccount(User user,double money){
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Set<Client> clients = user.getClient();
		Iterator<Client> it = clients.iterator();
		Client client = new Client();
		if(it.hasNext())
		{
			client = it.next();
		}
		Set<Account> accounts = client.getAccount();
		Iterator<Account> it1 = accounts.iterator();
		Account account = new Account();
		if(it1.hasNext())
		{
			account = it1.next();
		}
		double newBalance = account.getBalance()+money;
		account.setBalance(newBalance);
		account.setClient(client);
		Transaction tx = session.beginTransaction();
		try {
			session.update(account);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}


}


